package com.atguigu.lease.web.app.service.impl;


import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.app.mapper.*;
import com.atguigu.lease.web.app.service.*;
import com.atguigu.lease.web.app.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.app.vo.attr.AttrValueVo;
import com.atguigu.lease.web.app.vo.fee.FeeValueVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.atguigu.lease.web.app.vo.room.RoomDetailVo;
import com.atguigu.lease.web.app.vo.room.RoomItemVo;
import com.atguigu.lease.web.app.vo.room.RoomQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
@Slf4j
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {

    @Autowired
    ApartmentInfoService apartmentInfoService;
    @Autowired
    RoomAttrValueService roomAttrValueService;
    @Autowired
    RoomFacilityService roomFacilityService;
    @Autowired
    RoomLabelService roomLabelService;
    @Autowired
    RoomPaymentTypeService roomPaymentTypeService;
    @Autowired
    RoomLeaseTermService roomLeaseTermService;
    @Autowired
    GraphInfoService graphInfoService;
    @Autowired
    RoomAttrValueMapper roomAttrValueMapper;
    @Autowired
    RoomFacilityMapper roomFacilityMapper;
    @Autowired
    RoomLabelMapper roomLabelMapper;
    @Autowired
    RoomPaymentTypeMapper roomPaymentTypeMapper;
    @Autowired
    RoomLeaseTermMapper roomLeaseTermMapper;

    @Override
    public IPage<RoomItemVo> selectByPageInfo(IPage<RoomItemVo> itemVoIPage, RoomQueryVo queryVo) {
        return baseMapper.selectPageList(itemVoIPage,queryVo);
    }

    @Override
    public IPage<RoomItemVo> selectByPageInfoByApartmentId(IPage<RoomItemVo> roomItemVoIPage, Long id) {
        return baseMapper.selectPageListByApartmentId(roomItemVoIPage,id);
    }

    @Override
    public RoomDetailVo getDetailById(Long id) {
        //返回的对象
        RoomDetailVo roomDetailVo = new RoomDetailVo();

        //返回房子主信息
        RoomInfo roomInfo = baseMapper.selectById(id);
        BigDecimal minRent = roomInfo.getRent();
        BeanUtils.copyProperties(roomInfo,roomDetailVo);
        //返回属性信息
        List<AttrValueVo> attrValueVoList =roomAttrValueMapper.selectListByCondition(id);
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        //返回配套信息
        List<FacilityInfo> roomFacilityList = roomFacilityMapper.selectListByCondition(id);
        roomDetailVo.setFacilityInfoList(roomFacilityList);
        //返回标签信息
        List<LabelInfo> labelInfoList = roomLabelMapper.selectListByCondition(id);
        roomDetailVo.setLabelInfoList(labelInfoList);
        //返回支付方式
        List<PaymentType> paymentTypeList = roomPaymentTypeMapper.selectListByCondition(id);
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        //返回可选租期
        List<LeaseTerm> leaseTermList = roomLeaseTermMapper.selectListByCondition(id);
        roomDetailVo.setLeaseTermList(leaseTermList);
        //返回图片
        List<GraphVo> graphVoList = new ArrayList<>();
        List<GraphInfo> graphInfoList = graphInfoService.list(new LambdaQueryWrapper<>(GraphInfo.class).eq(GraphInfo::getItemId, id));
        for (GraphInfo graphInfo : graphInfoList) {
            GraphVo graphVo = new GraphVo(graphInfo.getName(), graphInfo.getUrl());
            graphVoList.add(graphVo);
        }
        //返回公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoService.getById(roomInfo.getApartmentId());
        ApartmentItemVo apartmentItemVo = new ApartmentItemVo();
        BeanUtils.copyProperties(apartmentInfo,apartmentItemVo);
        apartmentItemVo.setMinRent(minRent);
        apartmentItemVo.setLabelInfoList(labelInfoList);
        apartmentItemVo.setGraphVoList(graphInfoList);
        roomDetailVo.setApartmentItemVo(apartmentItemVo);
        roomDetailVo.setGraphVoList(graphVoList);

        return roomDetailVo;
    }



}




